//
//  PYShenheCell.swift
//  SheYangBigData
//
//  Created by 宋海胜 on 2021/1/8.
//  Copyright © 2021 叁拾叁. All rights reserved.
//

import UIKit

class PYMyDetailCell: PYCell {

    lazy var deleteBtn: UIButton = {
        let v: UIButton  = UIButton()
        v.setTitle("删除", for: .normal)
        v.setTitleColor(UIColor(hex: 0x3F71EF), for: .normal)
        v.titleLabel?.font = 12.autoFontSize()
        v.layer.cornerRadius = 13.5
        v.isHidden = true
        v.addAction {[weak self] in
            if let `self` = self {
                DispatchQueue.main.async {
                    self.deleteAction()
                }
            }
            
        }
        return v
    }()
    
    func configureShenheCell(urls: [String], model: PYMyDetailModel, isVideo: Bool) {
        self.configureMyDetailCell(urls: urls, model: model, isVideo: isVideo)
        deleteBtn.isHidden = true
    }
    
    func configureMyDetailCell(urls: [String], model: PYMyDetailModel, isVideo: Bool) {
        deleteBtn.isHidden = false
        
        
        var m = NSRecordsModel()
        m.id = model.id
        self.model = m
        
        
        if  model.hasDot > 0 {
            
            zanBtn.isSelected = true
        } else {
            
            zanBtn.isSelected = false
        }
        
        head.kf.setImage(with: URL(string: String(format: "%@%@", IMGIP, model.appUser?.headImg ?? "")))
        nameLb.text = model.appUser?.trueName
        contentLb.text = model.content
        timeLb.text = model.createDate
        sixinBtn.textLb.text = "私信 \(model.chatNum)"
        zanBtn.textLb.text = "点赞 \(model.dotNum)"
        zanNum = model.dotNum
        configureImageViews(urls: urls, isVideo: isVideo)
        
    }
    
    override func initViews() {
        super.initViews()
        self.sixinBtn.isHidden = true
        self.zanBtn.isHidden = true
        
        contentView.addSubview(deleteBtn)
        deleteBtn.snp.makeConstraints { (make) in
            make.centerY.equalTo(timeLb)
            make.right.equalTo(self).offset(5.0.autoWidth())
            make.width.equalTo(65.0.autoWidth())
            make.height.equalTo(12.0.autoWidth())
        }
    }

    func deleteAction() {
        
        let alert = PYDeleteAlert()
        alert.clickedClosure = { [weak self] index in
            if let `self` = self {
                if index == 1 {
                    
                    if let vc = self.currentVC as? PYMySinglePyqDetailViewController {
                        vc.requestDelete(id: Int(self.model.id ?? "") ?? 0, complete: { (success) in
                            if success {

                                vc.sy_toast(Constant_Toast.deleteSuccess) { (comp) in
                                    vc.backClosure?(0)
                                    vc.popVC()
                                }
                            }
                        })
                        
                    }
                }
            }
            
        }
        let window = UIApplication.shared.keyWindow
        window?.addSubview(alert)
        alert.snp.makeConstraints { (make) in
            make.edges.equalToSuperview()
        }
    }
}
